#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the version 3 of the GNU Lesser General Public License
#   as published by the Free Software Foundation.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU Lesser General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Copyright (c) NEC Deutschland GmbH, NEC HPC Europe

import unittest
import shutil

from aggmon.databases.metric import MetricDatabase
from basic_types.metric import Metric
from aggmon.databases.metric.rrd import NS_IN_S
from mon_hierarchy import Hierarchy

# Constants
TEST_HOST_NAME = "/test_host_name"
TEST_METRIC_NAME = "test_metric_name"
TEST_METRIC_SOURCE = "test_metric_source"
TEST_METRIC_VALUE = 12.0
TEST_METRIC_TIME = 1291646200


class MetricDatabaseTest(unittest.TestCase):
    def setUp( self ):
        hierarchy = Hierarchy( hostpath=TEST_HOST_NAME, config_file="metric_database_test.conf", verbose=True )
        hierarchy.print_info()
        self.db = MetricDatabase( "/tmp/metric_database_test", hierarchy=hierarchy )

    def tearDown( self ):
        self.db = None
        shutil.rmtree( "/tmp/metric_database_test" )


class MetricDatabaseAddMetricTest(MetricDatabaseTest):
    def runTest( self ):
        try:
            metric = Metric( TEST_METRIC_NAME, TEST_METRIC_SOURCE, host=TEST_HOST_NAME, time=TEST_METRIC_TIME, value=TEST_METRIC_VALUE )
            self.db.addMetric( metric )
            #self.fail()
        except AssertionError:
            raise
        except:
            pass


class MetricDatabaseGetLastMetricsByHostNameTest(MetricDatabaseTest):
    def runTest( self ):
        metric = Metric( TEST_METRIC_NAME, TEST_METRIC_SOURCE, host=TEST_HOST_NAME, time=TEST_METRIC_TIME, value=TEST_METRIC_VALUE )
        self.db.addMetric( metric )
        metrics = self.db.getLastMetricsByHostName( TEST_HOST_NAME )
        assert len( metrics ) == 1
        assert metrics[0].name == TEST_METRIC_NAME
        assert metrics[0].source == TEST_METRIC_SOURCE
        assert metrics[0].value == TEST_METRIC_VALUE
        assert metrics[0].time == TEST_METRIC_TIME
        print metrics[0]
        print metric


class MetricDatabaseGetRecordsByMetricNameTest(MetricDatabaseTest):
    def runTest( self ):
        metric = Metric( TEST_METRIC_NAME, TEST_METRIC_SOURCE, host=TEST_HOST_NAME, time=TEST_METRIC_TIME, value=TEST_METRIC_VALUE )
        self.db.addMetric( metric )
        rrd_records = self.db.getRecordsByMetricName( TEST_HOST_NAME, TEST_METRIC_NAME, start_s=0)
        assert len( rrd_records ) == 1
        assert rrd_records[0].value == TEST_METRIC_VALUE
        assert rrd_records[0].time_ns / NS_IN_S == TEST_METRIC_TIME


suite = unittest.TestSuite()
suite.addTest( MetricDatabaseAddMetricTest() )
suite.addTest( MetricDatabaseGetLastMetricsByHostNameTest() )
suite.addTest( MetricDatabaseGetRecordsByMetricNameTest() )
